/*
 *
 * Copyright (c) 2021
 * User:Dybala
 * File:AdminController.java
 * Date:2021/11/21 23:08:21
 */

package com.controller.AdminController;

import com.alibaba.excel.EasyExcel;
import com.config.myAnnotation.Log;
import com.config.myAnnotation.PassToken;
import com.pojo.*;
import com.service.AdminService;
import com.service.redisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 管理员控制类
 */
@Api(tags="管理员接口")
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    redisService server;

    /**
     * 登录,这里加入response只是为了快速获取token，方便后端调试
     * @return
     */
    @CrossOrigin
    @ApiOperation("管理员登录接口")
    @ApiResponses({
            @ApiResponse(code=401,message="登陆失败"),
            @ApiResponse(code=200,message="注册成功")
    })
    @PostMapping("/login")
    @PassToken
    public Message login(@RequestBody Admin admin, HttpServletResponse response, HttpServletRequest request){
        String token = request.getHeader("token");
        Message res = adminService.login(admin,token);
        if(res.getFlag()!=200){
            return res;

        }else{
            //校验成功，加入cookie
            Cookie cookie = new Cookie("token",res.getMsg());
            // 返回token
            response.addCookie(cookie);
        }
        return res;
    }

    /**
     * 等出
     * @return
     */
    @CrossOrigin
    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
        String token = request.getHeader("token");
        server.del("sys:admin:token" + token);
        return "已退出登录";
    }

    @CrossOrigin
    @ApiOperation("导出日志excel")
    @Log("导出日志excel")
    @GetMapping("/exportLog")
    public void exportLog(){
        adminService.exportLog();
    }

    @CrossOrigin
    @ApiOperation("导出用户表excel")
    @Log("导出用户表excel")
    @GetMapping("/exportUser")
    public void exportUser(){
        adminService.exportUser();
    }

    /**
     * 获取各类待办事务
     * @param type_id 事务编号 0：投诉 1：举报 2：申述
     * @return
     */
    @CrossOrigin
    @ApiOperation("分类所有事务")
    @Log("分类列出所有事务")
    @GetMapping("/getAffair")
    @PassToken
    public List<Report> getAffair(@RequestParam int type_id){
        //分类获取不同待办事务
        List<Report> report = adminService.getReport(type_id);
        return report;
    }

    @CrossOrigin
    @ApiOperation("封禁帐号")
    @Log("封禁帐号")
    @PostMapping("/lock")
    @PassToken
    public String lock(@RequestBody Map<String, String> map){
        int id = Integer.parseInt(map.get("id"));
        String email = map.get("email");
        adminService.lockUser(email);
        adminService.deleteAffairById(id);
        return "操作成功";
    }

    @CrossOrigin
    @ApiOperation("封禁帐号同时删除帖子")
    @Log("封禁帐号同时删除")
    @PostMapping("/lock2")
    @PassToken
    public String lock2(@RequestBody Map<String, String> map){
        int id = Integer.parseInt(map.get("id"));
        String email = map.get("email");
        String task_id = map.get("task_id");
        adminService.delTask(Integer.parseInt(task_id));
        adminService.lockUser(email);
        adminService.deleteAffairById(id);
        return "操作成功";
    }

    @CrossOrigin
    @ApiOperation("忽略请求")
    @Log("忽略请求帐号")
    @PostMapping("/ignore")
    @PassToken
    public String ignore(@RequestBody Map<String, String> map){
        int id = Integer.parseInt(map.get("id"));
        adminService.ignore(id);
//        adminService.deleteAffairById(id);
        return "操作成功";
    }

    @CrossOrigin
    @ApiOperation("删除帖子")
    @Log("删除帖子")
    @PostMapping("/clear")
    @PassToken
    public String clear(@RequestBody Map<String, String> map){
        int task_id = Integer.parseInt(map.get("id"));
        adminService.delTask(task_id);
        return "操作成功";
    }

    @CrossOrigin
    @ApiOperation("恢复账号")
    @Log("恢复账号")
    @PostMapping("/recover")
    @PassToken
    public String recover(@RequestBody Map<String, String> map){
        int id = Integer.parseInt(map.get("id"));
        String email = map.get("email");
        adminService.unlockUser(email);
        adminService.deleteAffairById(id);
        return "操作成功";
    }
    @CrossOrigin
    @ApiOperation("发布公告")
//    @Log("发布公告")
    @PostMapping("/publishNotice")
//    @PassToken
    public String publishNotice(@RequestBody Notice notice){
        if(notice.getTitle()!=null){
            if(adminService.publishNotice(notice)){return "发布成功";}
            else return "发布失败，请联系运维人员";
        }
        else return "发布失败,请检查标题是否为空";
    }

    @CrossOrigin
    @ApiOperation("删除公告")
    @Log("删除公告")
    @PostMapping("/delNotice")
    public String delNotice(@RequestBody Map<String,String>map){
        String nid=map.getOrDefault("nid","-1");
        if(adminService.delNotice(Integer.parseInt(nid))){
            return "删除成功";
        }
        else return "删除失败";
    }

    @CrossOrigin
    @ApiOperation("初始化管理员界面")
//    @Log("删除公告")
    @GetMapping("/adminInit")
    public Map<String,Integer> adminInit(){
        Map<String, Integer> map = adminService.initAdmin();
        return map;
    }

}
